﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Nova_Common;

namespace Nova_BLL
{
    public class RF
    {
        Nova_DAL.RF dalRF = new Nova_DAL.RF();

        DataSet ds = null;
        /// <summary>
        /// 获取托盘组盘的物料信息
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetTrayMaterialInfor(string strTrayId, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;

            return dalRF.GetTrayMaterialInfor(strTrayId, out ds, out strMessage);
        }

        public int GetTrayMaterialInforNF(string strTrayId, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;

            return dalRF.GetTrayMaterialInforNF(strTrayId, out ds, out strMessage);
        }


        /// <summary>
        /// 根据物料编码获取物料的信息
        /// </summary>
        /// <param name="materialCode"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetMaterialInfor(string materialCode, out DataSet ds, out string strMessage)
        {
            strMessage = "";
            ds = null;

            return dalRF.GetMaterialInfor(materialCode, out ds, out strMessage);
        }

        /// <summary>
        /// 组盘功能（添加）
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败； 1：成功</returns>
        public int RFPackage(string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            try
            {   //判断数据是正整形
                if (MatNum <= 0)
                {
                    strMessage = "物料组盘数量必须大于0";
                    return -1;
                }
                // 判断strTrayId、materialCode是否有效，能否允许组盘
                if (dalRF.TrayCanPackage(strTrayId, out strMessage) != 1)
                {

                    return -1;
                }
                if (dalRF.MaterialCanPackage(materialCode, out strMessage) != 1)
                {

                    return -1;
                }
                //根据物料编码获取物料内部编号
                long SecMatId = 0;
                if (dalRF.GetSecMatIdByMateCode(materialCode, out SecMatId, out strMessage) == -1)
                {
                    return -1;
                }
                //执行组盘
                return dalRF.Package(strTrayId, SecMatId, MatNum, out strMessage);



            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        public int RFPackageNF(string strStockNo, string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            try
            {   //判断数据是正整形
                if (MatNum <= 0)
                {
                    strMessage = "物料组盘数量必须大于0";
                    return -1;
                }
                // 判断strTrayId、materialCode是否有效，能否允许组盘
                if (dalRF.TrayCanPackage(strTrayId, out strMessage) != 1)
                {

                    return -1;
                }
                if (dalRF.MaterialCanPackage(materialCode, out strMessage) != 1)
                {

                    return -1;
                }

                //判断组盘的数量加上实际数量不能超过订单的入库数量
                //已在组盘存储过程中


                //根据物料编码获取物料内部编号
                long SecMatId = 0;
                if (dalRF.GetSecMatIdByMateCode(materialCode, out SecMatId, out strMessage) == -1)
                {
                    return -1;
                }
                //执行组盘
                return dalRF.PackageCommonNF(strStockNo, materialCode, strTrayId, SecMatId, MatNum, out strMessage);



            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }



        /// <summary>
        /// 执行组盘--含尺寸、重量类型
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int RFPackageCommon(string strTrayId, string materialCode, int MatNum, int TypeOfSize, int TypeOfWeight, out string strMessage)
        {
            strMessage = "";
            try
            {   //判断数据是正整形
                if (MatNum <= 0)
                {
                    strMessage = "物料组盘数量必须大于0";
                    return -1;
                }
                // 判断strTrayId、materialCode是否有效，能否允许组盘
                if (dalRF.TrayCanPackage(strTrayId, out strMessage) != 1)
                {

                    return -1;
                }
                if (dalRF.MaterialCanPackage(materialCode, out strMessage) != 1)
                {

                    return -1;
                }
                //根据物料编码获取物料内部编号
                long SecMatId = 0;
                if (dalRF.GetSecMatIdByMateCode(materialCode, out SecMatId, out strMessage) == -1)
                {
                    return -1;
                }



                //执行组盘--含尺寸、重量类型
                return dalRF.PackageCommon( strTrayId, SecMatId, MatNum, TypeOfSize, TypeOfWeight, out strMessage);

               

            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }


        /// <summary>
        /// 与组盘逻辑增加差别：盘点补货 增加 不需要判断托盘是否在库里
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int RFCheckManageAdd(string strTrayId, string materialCode, int MatNum, int TypeOfSize, int TypeOfWeight, out string strMessage)
        {
            strMessage = "";
            try
            {   //判断数据是正整形
                if (MatNum <= 0)
                {
                    strMessage = "物料组盘数量必须大于0";
                    return -1;
                }
                //盘点补货 增加 不需要判断托盘是否在库里
                //// 判断strTrayId、materialCode是否有效，能否允许组盘
                //if (dalRF.TrayCanPackage(strTrayId, out strMessage) != 1)
                //{
                //    return -1;
                //}

                if (dalRF.MaterialCanPackage(materialCode, out strMessage) != 1)
                {

                    return -1;
                }
                //根据物料编码获取物料内部编号
                long SecMatId = 0;
                if (dalRF.GetSecMatIdByMateCode(materialCode, out SecMatId, out strMessage) == -1)
                {
                    return -1;
                }
                //执行组盘--含尺寸、重量类型
                return dalRF.PackageCommon(strTrayId, SecMatId, MatNum, TypeOfSize, TypeOfWeight, out strMessage);

            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }




        /// <summary>
        /// 组盘数量的更新
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败；1：成功</returns>
        public int PackageNumUpdate(string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            string ContainerID = "";
            long Sec_Mat_ID = 0;
            try
            {
                if (MatNum <= 0)
                {
                    strMessage = "物料数据更新数据必须大于0";
                    return -1;
                }
                //根据strTrayId, materialCode获取 ContainerID,Sec_Mat_ID
                if (dalRF.GetStorageKey(strTrayId, materialCode, out ContainerID, out Sec_Mat_ID, out strMessage) != 1)
                {
                    return -1;
                }
                //更新TBase_Storage 表中Mat_Num
                return dalRF.UpdateStorageNum(ContainerID, Sec_Mat_ID, MatNum, out strMessage);
            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        /// <summary>
        /// 组盘数量更新 NF
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageNumUpdateNF(string strStockNo,string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            string ContainerID = "";
            long Sec_Mat_ID = 0;
            try
            {
                if (MatNum <= 0)
                {
                    strMessage = "物料数据更新数据必须大于0";
                    return -1;
                }
                //根据strTrayId, materialCode获取 ContainerID,Sec_Mat_ID
                if (dalRF.GetStorageKey(strTrayId, materialCode, out ContainerID, out Sec_Mat_ID, out strMessage) != 1)
                {
                    return -1;
                }
                //更新TBase_Storage 表中Mat_Num和TAddStorage_List 中Act_Num
                return dalRF.UpdateStorageNumNF(strStockNo, ContainerID, Sec_Mat_ID, MatNum, out strMessage);
            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }


        /// <summary>
        /// 组盘的删除功能
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageDel(string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            string ContainerID = "";
            long Sec_Mat_ID= 0;
            try
            {
                if (MatNum <= 0)
                {
                    strMessage = "删除的物料数量必须大于0";
                    return -1;

                }
                //根据strTrayId, materialCode获取 ContainerID,Sec_Mat_ID
                if (dalRF.GetStorageKey(strTrayId, materialCode, out ContainerID, out Sec_Mat_ID, out strMessage) != 1)
                {
                    return -1;
                }
                //调用存储过程，完成删除功能
                return dalRF.PackageDel(ContainerID, Sec_Mat_ID, MatNum, out strMessage);

            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        /// <summary>
        /// 组盘删除功能 NF
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageDelNF(string StockNo, string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            string ContainerID = "";
            long Sec_Mat_ID = 0;
            try
            {
                if (MatNum <= 0)
                {
                    strMessage = "删除的物料数量必须大于0";
                    return -1;

                }
                //根据strTrayId, materialCode获取 ContainerID,Sec_Mat_ID
                if (dalRF.GetStorageKey(strTrayId, materialCode, out ContainerID, out Sec_Mat_ID, out strMessage) != 1)
                {
                    return -1;
                }
                //调用存储过程，完成删除功能
                return dalRF.PackageDelNF(StockNo,ContainerID, Sec_Mat_ID, MatNum, out strMessage);

            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }

        /// <summary>
        /// 组盘删除功能 NF 0220
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="materialCode"></param>
        /// <param name="MatNum"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        public int PackageDelNF_New(string StockNo, string strTrayId, string materialCode, int MatNum, out string strMessage)
        {
            strMessage = "";
            string ContainerID = "";
            long Sec_Mat_ID = 0;
            try
            {
                if (MatNum <= 0)
                {
                    strMessage = "删除的物料数量必须大于0";
                    return -1;

                }
                //根据strTrayId, materialCode获取 ContainerID,Sec_Mat_ID
                if (dalRF.GetStorageKey(strTrayId, materialCode, out ContainerID, out Sec_Mat_ID, out strMessage) != 1)
                {
                    return -1;
                }
                //调用存储过程，完成删除功能
                return dalRF.PackageDelNF_New(StockNo, ContainerID, Sec_Mat_ID, MatNum, out strMessage);

            }
            catch (Exception ex)
            {
                strMessage = "执行异常";
                return -1;
            }
        }


        /// <summary>
        /// 获取获取尺寸 表示名称、数值
        /// </summary>
        /// <returns></returns>
        public DataSet DataListSize()
        {
            return new TSysoption().DataListTSysoptionSize(109, 1);
        }


        /// <summary>
        /// 获取未处理单据信息
        /// </summary>
        /// <returns></returns>
        public DataSet DataListStorage()
        {
            try
            {
                ds = dalRF.DataListStorage();
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }



        /// <summary>
        /// 获取单据上物料信息
        /// </summary>
        /// <param name="StockNo"></param>
        /// <returns></returns>
        public DataSet DataListMaterial(string StockNo)
        {
            try
            {
                ds = dalRF.DataListMaterial(StockNo);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            return ds;
        }




        /// <summary>
        /// 获取单据上物料剩余数量
        /// </summary>
        /// <param name="StockNo"></param>
        /// <param name="MatCode"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>

        public int GetMaterialReNum(string StockNo, string MatCode, out string strMessage)
        {
            return dalRF.GetMaterialReNum(StockNo, MatCode, out strMessage);
        }


        /// <summary>
        ///  根据托盘码获取托盘尺寸类型
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetSizeByTrayId(string strTrayId, out int TypeOfSize, out string strMessage)
        {
            return dalRF.GetSizeByTrayId(strTrayId, out TypeOfSize, out strMessage);
        }

        /// <summary>
        /// pda 根据托盘号 获取出库指令展示信息 
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="ds"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:获取数据失败 0：无数据 1：获取数据成功</returns>
        public int GetPDAShow(string strTrayId, out DataSet ds, out string strMessage)
        {
            return dalRF.GetPDAShow(strTrayId, out ds, out strMessage);
        }


        /// <summary>
        /// PDA 过账完成
        /// </summary>
        /// <param name="TaskID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败；1：成功</returns>
        public int PDAUpdateCMDS(long TaskID, out string strMessage)
        {
            return dalRF.PDAUpdateCMDS(TaskID, out strMessage);
        }

        /// <summary>
        /// 设置空托盘的尺寸等信息
        /// </summary>
        /// <param name="strTrayId"></param>
        /// <param name="TypeOfSize"></param>
        /// <param name="TypeOfWeight"></param>
        /// <param name="TypeOfZone"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1：失败；1：成功</returns>
        public int SetSpaceTrayInfor(string strTrayId, int TypeOfSize, int TypeOfWeight, int TypeOfZone, out string strMessage)
        {
            return dalRF.SetSpaceTrayInfor(strTrayId, TypeOfSize, TypeOfWeight, TypeOfZone, out strMessage);
        }


        /// <summary>
        /// PDA产生入库指令
        /// </summary>
        /// <param name="TrayId"></param>
        /// <param name="SQIDS"></param>
        /// <param name="TaskID"></param>
        /// <param name="strMessage"></param>
        /// <returns>-1:失败 1：成功</returns>
        public int PDAGenerateInputCmd(string TrayId, Int16 SQIDS, out long TaskID, out string strMessage)
        {
            return dalRF.PDAGenerateInputCmd(TrayId, SQIDS, out TaskID, out  strMessage);
        }



    }
}
